iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 18
1
Software Development

C#可以做出甚麼?系列 第 18

我想還是實作

  • 分享至 

  • xImage
  •  

建立好版面
https://ithelp.ithome.com.tw/upload/images/20210404/20119035AltwoYs3u1.png

開好
https://ithelp.ithome.com.tw/upload/images/20210404/20119035IU9Fhof0Ga.png

裡面的作用
https://ithelp.ithome.com.tw/upload/images/20210404/2011903558wdfNY2px.png

物件=可以被定義=EX:給名稱-滑鼠=(屬性properties)藍色+按鍵

https://ithelp.ithome.com.tw/upload/images/20210404/201190358lCgBCDwlV.png

AUTOSIZE=可以調框的大小=false=才可以調大小

size=大小=寬+高
https://ithelp.ithome.com.tw/upload/images/20210404/20119035ZPXmQtr8hr.png

BORDERSTYLE=外框
https://ithelp.ithome.com.tw/upload/images/20210404/20119035KjqHvK3311.png

BACKCOLOR=框內背景色
https://ithelp.ithome.com.tw/upload/images/20210404/20119035nLuoC5BOdp.png

FORECOLOR=文字色彩
https://ithelp.ithome.com.tw/upload/images/20210404/201190359Mxi2Koioc.png

TEXT=顯示文字內容
https://ithelp.ithome.com.tw/upload/images/20210404/20119035swwwBLuMnu.png

textalign=字的對齊
https://ithelp.ithome.com.tw/upload/images/20210404/20119035cDMqDwsQqq.png

font=自型

location=X+Y去調位置

Enabled=啟用=TRUE 是否有作用
https://ithelp.ithome.com.tw/upload/images/20210404/20119035Kzmsi3mccm.png

跑出來的視窗
https://ithelp.ithome.com.tw/upload/images/20210404/20119035PN1NDDOXxv.png

visible=可見狀態
locked=鎖定大小及位置


(重要)NAME=控制項物件名稱=EX:滑鼠
1.半形英文/數字/底線
2.不可以僅有數字/數字開頭
3.英文大小寫是不同
4.匈牙利命名btn(按鈕)_CalculatorKeyNum_9
5.駝峰命名

目前這樣是Name可以變

https://ithelp.ithome.com.tw/upload/images/20210405/201190352Rre3S29m1.png


視窗縮小放大按鈕-要點form-選不是按字母排序才看得到
controlbox連要不要X都無法點

https://ithelp.ithome.com.tw/upload/images/20210405/20119035rCEX0etiqD.png


"閃電"-load=當表單出現時要做的狀態

https://ithelp.ithome.com.tw/upload/images/20210405/20119035fye8udTATI.png


檢視程式碼:

https://ithelp.ithome.com.tw/upload/images/20210405/20119035Y7F7KBuAgy.png

程序-結構
https://ithelp.ithome.com.tw/upload/images/20210405/20119035lvENoOdBfe.png

程式編寫規則:
https://ithelp.ithome.com.tw/upload/images/20210405/20119035TRpBMlMkPr.png

1.結尾+;
2.大小寫相異
3.關鍵字+語法結構必須完全正確

程式語法:.=的
例如lab_Test.Text="哈哈哈";
https://ithelp.ithome.com.tw/upload/images/20210405/20119035OB9k3AjfBy.png


使用Color類別

lab_Test.ForeColor = Color.Yellow;
lab_Test.BackColor = Color.Green;
https://ithelp.ithome.com.tw/upload/images/20210405/20119035qyp89Y1uaR.png

也可以使用指定ARG的值=三原色色彩.範圍0-255
lab_Test.ForeColor = Color.FromArgb(0,255,0);
https://ithelp.ithome.com.tw/upload/images/20210405/20119035M4ZaZXsC1E.png


Textbox讓使用者輸入文字
https://ithelp.ithome.com.tw/upload/images/20210405/20119035L2x923u9uU.png

啟用多行MultiLine+捲軸功能ScrollBars=用BOTH讓他自己選
https://ithelp.ithome.com.tw/upload/images/20210406/20119035CfcwGjQgRF.png

用BOTH讓他自己選要上下還是左右延伸
https://ithelp.ithome.com.tw/upload/images/20210406/20119035IypXnrdx3q.png

MaxLength可以輸入的字
https://ithelp.ithome.com.tw/upload/images/20210406/20119035V6YN3kFAAm.png

Passwordchar=密碼字元.如果打顯示打出的字都是空格的話就是會顯示打出的字
https://ithelp.ithome.com.tw/upload/images/20210406/20119035bue5eb7K15.png

https://ithelp.ithome.com.tw/upload/images/20210406/20119035zH0T8YEAxy.png

readonly=唯讀狀態
right to left=打字方向


編輯FROM的NAME和CONTROLBOX變FALSE=關掉放大+縮小+關閉 功能

https://ithelp.ithome.com.tw/upload/images/20210406/20119035qzI4BKpu9I.png

https://ithelp.ithome.com.tw/upload/images/20210405/20119035tIiLuXgJ5N.png

https://ithelp.ithome.com.tw/upload/images/20210406/20119035r8A2peUfOm.png


開始幫Textbox命名name+text成tbx_FN

https://ithelp.ithome.com.tw/upload/images/20210406/20119035DSd7zyG8g0.png

https://ithelp.ithome.com.tw/upload/images/20210406/20119035xHtVv8Akae.png

RadioButton=多選一
https://ithelp.ithome.com.tw/upload/images/20210406/20119035kNRTGUad1d.png

checked如果是true代表預設被選到
https://ithelp.ithome.com.tw/upload/images/20210406/20119035xetG10qeXd.png

checkAlign=核取狀態被選到


使用Groupbox把原來的血型/性別刪掉.再把各自框住-直接執行-就可以分開各自選取1個-
用text去改顯示的字

https://ithelp.ithome.com.tw/upload/images/20210406/20119035U8DwkC69XF.png


checkbox-多選-

https://ithelp.ithome.com.tw/upload/images/20210406/20119035xXH4solSM8.png

threestate-用於選擇時比較沒有那麼確定時.不是V/或空格是方格..新版才有
https://ithelp.ithome.com.tw/upload/images/20210407/20119035myCC446Ves.png


button鍵
做動-

https://ithelp.ithome.com.tw/upload/images/20210407/20119035vqivHe4c3T.png

autosize取消可以改大小+borderstyle改single是單線
https://ithelp.ithome.com.tw/upload/images/20210407/20119035QAM3xYunR6.png


紀念性的一刻~打了兩次...終於沒有手癢"觸動click=滑鼠按兩下"
https://ithelp.ithome.com.tw/upload/images/20210407/20119035z5SX9K9Pc7.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _20210407
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Btn_Opt_Click(object sender, EventArgs e)
        {
            lab_Opt.Text = "按鈕已按下";
        }
    }
}

然後要讓按下有顯示語法新增

private void Btn_Opt_MouseDown(object sender, EventArgs e)
        {
            lab_Opt.Text = "按鈕已按下";
        }


code刪掉的方法:不然會顯示錯誤=空白
把反紅的刪掉就正常...重要
https://ithelp.ithome.com.tw/upload/images/20210407/201190355JxkmFVGtW.png
把反紅的刪掉就正常...重要/images/emoticon/emoticon02.gif
https://ithelp.ithome.com.tw/upload/images/20210407/20119035x5x4GGjaLF.png

回到正常
https://ithelp.ithome.com.tw/upload/images/20210407/20119035mufLkIBRX8.png

https://ithelp.ithome.com.tw/upload/images/20210407/201190357DTHMDvIA2.png


變數:承載資料容器=記憶體位置
宣告變數語法:
變數型態 變數名稱:

int iNum1:

整數=沒有小數
浮點數=有小數
字元
字串
布林=true/false


這裡輸出的都是字串 用string

1.取得姓名/text都是字串
2.需要字串string 產生strOpt變數=容器
3.在strOpt裡去乘載姓名tbx_FN.Text就是姓名的文字內容
4.所以呈現的lab_Opt.Text=strOpt 下面客格的區域的區域去乘載變數strOpt


運算值:
+:10+25=兩數相加
如果是10+"QOO"=10QOO
%餘數
28%5=3
=是指定.將右方的內容指定給左方
\n換行

判斷語法:比較值=

比較對象 比較方式 比較值
!Age >= 18

==等於

!=不等於

"" 空格


單一判斷的語法
if(判斷條件)
{
條件的結果;
}


不是空格才可以輸出姓名

二分法判斷

if(判斷條件)
{
條件符合的結果;
}
else
{
條件不符合的結果;
}
https://ithelp.ithome.com.tw/upload/images/20210409/20119035jttFilotMY.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _20210407
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Btn_Opt_Click(object sender, EventArgs e)
        {
            string strOpt = "";
            if (tbx_FN.Text != "")
            { strOpt = "姓名" + tbx_FN.Text + "\n"; }


            if (rdbtn_BTA.Checked == true)
            { strOpt = strOpt + "血型" + rdbtn_BTA.Text + "\n"; }
            else if (rdbtn_BTB.Checked == true)
            { strOpt = strOpt + "血型" + rdbtn_BTB.Text + "\n"; }
            else if (rdbtn_BTO.Checked == true)
            { strOpt = strOpt + "血型" + rdbtn_BTO.Text + "\n"; }
            else 
            { strOpt = strOpt + "血型" + rdbtn_BTAB.Text + "\n"; }

            if (rdbtn_GDM.Checked == true)
            {
                strOpt = strOpt + "性別:" + rdbtn_GDM.Text + "\n";
            }
            else
            {
                strOpt = strOpt + "性別" + rdbtn_GDF.Text + "";

            }

            lab_Opt.Text = strOpt;

        }
    }
}


strOpt +改成+= 是
OP運算會被改寫成
a=a+2 -> a+=2
a=a-3 -> a-=3
a=a7 -> a=7
a=a/5 -> a/=5
a=a%9 -> a%=9

重要
a+=1 -> ++a(本行+1) a++(次行+1)
a-=1 -> --a(本行-1) a--(次行-1)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _20210407
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Btn_Opt_Click(object sender, EventArgs e)
        {
            string strOpt = "";
            if (tbx_FN.Text != "")
            { strOpt = "姓名" + tbx_FN.Text + "\n"; }


            if (rdbtn_BTA.Checked == true)
            { strOpt += "血型" + rdbtn_BTA.Text + "\n"; }
            else if (rdbtn_BTB.Checked == true)
            { strOpt += "血型" + rdbtn_BTB.Text + "\n"; }
            else if (rdbtn_BTO.Checked == true)
            { strOpt += "血型" + rdbtn_BTO.Text + "\n"; }
            else 
            { strOpt += "血型" + rdbtn_BTAB.Text + "\n"; }

            if (rdbtn_GDM.Checked == true)
            {
                strOpt += "性別:" + rdbtn_GDM.Text + "\n";
            }
            else
            {
                strOpt += "性別" + rdbtn_GDF.Text + "";

            }

            lab_Opt.Text = strOpt;

        }
    }
}


巢狀判斷


https://ithelp.ithome.com.tw/upload/images/20210408/20119035922lL6DxFk.png

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _20210407
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Btn_Opt_Click(object sender, EventArgs e)
        {
            string strOpt = "";
            if (tbx_FN.Text != "")
            { strOpt = "姓名" + tbx_FN.Text + "\n"; }

            if (rdbtn_GDM.Checked == true)
            {
                strOpt = strOpt + "性別:" + rdbtn_GDM.Text + "\n";
            }
            else
            {
                strOpt = strOpt + "性別" + rdbtn_GDF.Text + "";

            }

            lab_Opt.Text = strOpt;

        }
    }
}




線性判斷
if(判斷條件1)
{
條件1符合的結果;
}
else if(判斷條件2)
{
條件2符合的結果;
}
else
{
上述結果都不符合
}


巢狀迴圈:判斷後再判斷=先判斷是否18歲..然後再判斷是否住在台北
if(iAge>=18)
{
if(iCity=="台北")
{tab_Opt.Text="恭喜您可加入台北市民卡會員"}
else
{tab_Opt.Text="居住地不符"}
}
else
{tab.Opt.Text="未成年,無法加入會員";}


複數條件判斷
1.&&且

找出1-1000

if(iVal>=0 && iVal<=1000)

2.||或
找出1或3或5的結果
if(iVal ==1 ||iVal ==3 ||iVal ==5 )

3.!將結果相反

不小於18
if(!(iAge<18))


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _20210407
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Btn_Opt_Click(object sender, EventArgs e)
        {
            string strOpt = "";
            if (tbx_FN.Text != "")
            { strOpt = "姓名" + tbx_FN.Text + "\n"; }


            if (rdbtn_BTA.Checked == true)
            { strOpt += "血型" + rdbtn_BTA.Text + "\n"; }
            else if (rdbtn_BTB.Checked == true)
            { strOpt += "血型" + rdbtn_BTB.Text + "\n"; }
            else if (rdbtn_BTO.Checked == true)
            { strOpt += "血型" + rdbtn_BTO.Text + "\n"; }
            else 
            { strOpt += "血型" + rdbtn_BTAB.Text + "\n"; }

            if (rdbtn_GDM.Checked == true)
            {
                strOpt += "性別:" + rdbtn_GDM.Text + "\n";
            }
            else
            {
                strOpt += "性別" + rdbtn_GDF.Text + "\n";

            }

            if(ckbx_1.Checked==true || ckbx_2.Checked==true || ckbx_3.Checked==true
                ||ckbx_4.Checked==true||ckbx_5.Checked==true|| ckbx_6.Checked==true)
            {
                strOpt += "興趣";
                if (ckbx_1.Checked == true) { strOpt += ckbx_1.Text + ""; }
                if (ckbx_2.Checked == true) { strOpt += ckbx_2.Text + ""; }
                if (ckbx_3.Checked == true) { strOpt += ckbx_3.Text + ""; }
                if (ckbx_4.Checked == true) { strOpt += ckbx_4.Text + ""; }
                if (ckbx_5.Checked == true) { strOpt += ckbx_5.Text + ""; }
                if (ckbx_6.Checked == true) { strOpt += ckbx_6.Text + ""; }
            }




            lab_Opt.Text = strOpt;

        }
    }
}


要用checkbox才能複選
https://ithelp.ithome.com.tw/upload/images/20210409/20119035YUVRGkt932.png

https://ithelp.ithome.com.tw/upload/images/20210409/20119035et0CilPiWT.png


下拉式選單 combBox
https://ithelp.ithome.com.tw/upload/images/20210409/201190357H3UR1aFWs.png

從item旁的三個點按輸入1-12月才可以選
https://ithelp.ithome.com.tw/upload/images/20210409/20119035GXAJVdy9qv.png

https://ithelp.ithome.com.tw/upload/images/20210409/20119035AyRlK7oSMA.png

https://ithelp.ithome.com.tw/upload/images/20210409/201190350Y6guda7G9.png

用語法輸入

private void Form1_Load(object sender, EventArgs e)
        {

        }
    ````    
的語法出式按 Form+閃電->找到    Load點兩下
![https://ithelp.ithome.com.tw/upload/images/20210409/20119035hrJwpExFlm.png](https://ithelp.ithome.com.tw/upload/images/20210409/20119035hrJwpExFlm.png)

用程式碼加入1

    private void Form1_Load(object sender, EventArgs e)
    {
        cbx_BDM.Items.Add(1);
    }

![https://ithelp.ithome.com.tw/upload/images/20210409/20119035F8OIA3ZbzY.png](https://ithelp.ithome.com.tw/upload/images/20210409/20119035F8OIA3ZbzY.png)

sorted排序功能..排過只能手改



-----

迴圈:
1.重複執行:
2.固定變化規律值
3.指定程式執行次數

for迴圈

語法:

變數型態 變數名稱:

for(起始狀態或值;迴圈執行條件;起始狀態或計數值變化的方式i++下一行+1)
{
迴圈程式區設
}


-----
int i;

            for (i = 1; i <= 12; i++)
            {
                cbx_BDM.Items.Add(i);


            }


![https://ithelp.ithome.com.tw/upload/images/20210409/20119035GuCTwtpbgF.png](https://ithelp.ithome.com.tw/upload/images/20210409/20119035GuCTwtpbgF.png)
-----
年月日規則

年:今年到130年前

int iYoT=DateTime.Today.Year;

        for (i = iYoT; i>= iYoT-130; i--)
        {
            cbx_BDY.Items.Add(i);
        }

月:1-12月
日:大月31天=1+3+5+7+8+10+12
小月30天=4+6+9+11
閏年29天.非閏年28天.
閏年規則可以被400整除+被4整除+不可與100整除

使用日期函數
![https://ithelp.ithome.com.tw/upload/images/20210409/20119035S4buQjyg70.png](https://ithelp.ithome.com.tw/upload/images/20210409/20119035S4buQjyg70.png)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _20210407
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Btn_Opt_Click(object sender, EventArgs e)
    {
        string strOpt = "";
        if (tbx_FN.Text != "")
        { strOpt = "姓名" + tbx_FN.Text + "\n"; }


        if (rdbtn_BTA.Checked == true)
        { strOpt += "血型" + rdbtn_BTA.Text + "\n"; }
        else if (rdbtn_BTB.Checked == true)
        { strOpt += "血型" + rdbtn_BTB.Text + "\n"; }
        else if (rdbtn_BTO.Checked == true)
        { strOpt += "血型" + rdbtn_BTO.Text + "\n"; }
        else
        { strOpt += "血型" + rdbtn_BTAB.Text + "\n"; }

        if (rdbtn_GDM.Checked == true)
        {
            strOpt += "性別:" + rdbtn_GDM.Text + "\n";
        }
        else
        {
            strOpt += "性別" + rdbtn_GDF.Text + "\n";

        }

        if (ckbx_1.Checked == true || ckbx_2.Checked == true || ckbx_3.Checked == true
            || ckbx_4.Checked == true || ckbx_5.Checked == true || ckbx_6.Checked == true)
        {
            strOpt += "興趣";
            if (ckbx_1.Checked == true) { strOpt += ckbx_1.Text + ""; }
            if (ckbx_2.Checked == true) { strOpt += ckbx_2.Text + ""; }
            if (ckbx_3.Checked == true) { strOpt += ckbx_3.Text + ""; }
            if (ckbx_4.Checked == true) { strOpt += ckbx_4.Text + ""; }
            if (ckbx_5.Checked == true) { strOpt += ckbx_5.Text + ""; }
            if (ckbx_6.Checked == true) { strOpt += ckbx_6.Text + ""; }
        }




        lab_Opt.Text = strOpt;

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        int i;

        for (i = 1; i <= 12; i++)
        {
            cbx_BDM.Items.Add(i);
        }

        int iYoT=DateTime.Today.Year;

        for (i = iYoT; i>= iYoT-130; i--)
        {
            cbx_BDY.Items.Add(i);
        }


    }

}

}



-----
如果年要固定點開都是16歲看
就是今天年-16
  cbx_BDY.Text = (iYoT - 16);
  
  1.但是要整數轉字串
變數.ToString();
就是cbx_BDY.Text = (iYoT - 16).ToString();
![https://ithelp.ithome.com.tw/upload/images/20210410/2011903581dkK2OZqQ.png](https://ithelp.ithome.com.tw/upload/images/20210410/2011903581dkK2OZqQ.png)
2可以轉成整數的字串來源

  int.Prase();

將月份轉成整數
iMath = int.Parse(cbx_BDM.Text);


-----


switch(判斷對象)
{
case 可能值A:分號

break;中斷=沒有就會一直執行

}

產生不同月份的天數
大月是31天,小月是30天,但是2月份始終是28天或者29天,並且7月8月都是31天。


switch(iMth)
           {
               case 1:
       case 3:
       case 5:
       case 7:
       case 8:
       case 10:
       case 12:
           iDays = 31;
           break;


       case 4:
       case 6:
       case 9:
       case 11:
           iDays = 30;
           break;

       case 2:
           break;
       }


       }
       
       ````
       
目前使用的格里高利曆閏年規則如下:

公元年分非4的倍數,為平年,或
公元年分為4的倍數但非100的倍數,為閏年,或
公元年分為100的倍數但非400的倍數,為平年,或
公元年分為400的倍數為閏年。

-----

到這裡是有出現日期

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _20210407
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Btn_Opt_Click(object sender, EventArgs e)
    {
        string strOpt = "";
        if (tbx_FN.Text != "")
        { strOpt = "姓名" + tbx_FN.Text + "\n"; }


        if (rdbtn_BTA.Checked == true)
        { strOpt += "血型" + rdbtn_BTA.Text + "\n"; }
        else if (rdbtn_BTB.Checked == true)
        { strOpt += "血型" + rdbtn_BTB.Text + "\n"; }
        else if (rdbtn_BTO.Checked == true)
        { strOpt += "血型" + rdbtn_BTO.Text + "\n"; }
        else
        { strOpt += "血型" + rdbtn_BTAB.Text + "\n"; }

        if (rdbtn_GDM.Checked == true)
        {
            strOpt += "性別:" + rdbtn_GDM.Text + "\n";
        }
        else
        {
            strOpt += "性別" + rdbtn_GDF.Text + "\n";

        }

        if (ckbx_1.Checked == true || ckbx_2.Checked == true || ckbx_3.Checked == true
            || ckbx_4.Checked == true || ckbx_5.Checked == true || ckbx_6.Checked == true)
        {
            strOpt += "興趣";
            if (ckbx_1.Checked == true) { strOpt += ckbx_1.Text + ""; }
            if (ckbx_2.Checked == true) { strOpt += ckbx_2.Text + ""; }
            if (ckbx_3.Checked == true) { strOpt += ckbx_3.Text + ""; }
            if (ckbx_4.Checked == true) { strOpt += ckbx_4.Text + ""; }
            if (ckbx_5.Checked == true) { strOpt += ckbx_5.Text + ""; }
            if (ckbx_6.Checked == true) { strOpt += ckbx_6.Text + ""; }
        }




        lab_Opt.Text = strOpt;

    }

    private void Form1_Load(object sender, EventArgs e)
    {
      

        for (int i = 1; i <= 12; i++)
        {
            cbx_BDM.Items.Add(i);
        }

        int iYoT=DateTime.Today.Year;

        for (int i = iYoT; i >= iYoT - 130; i--)
        {
            cbx_BDY.Items.Add(i);
        }

        cbx_BDY.Text = (iYoT - 16).ToString();


        int iDays = 0, iMth = int.Parse(cbx_BDM.Text),iYr=0;

        switch(iMth)
        {
            case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        iDays = 31;
        break;


    case 4:
    case 6:
    case 9:
    case 11:
        iDays = 30;
        break;

    case 2:
                iYr = int.Parse(cbx_BDY.Text);
                if (iYr % 400 == 0 || (iYr % 4 == 0 && iYr % 100 != 0))
                { iDays = 29; }
                else
                { iDays = 28; }

                break;
    }

for(int i=1;i<=iDays;i++)
{
cbx_BDD.Items.Add(i);
}

    }

}

}


![https://ithelp.ithome.com.tw/upload/images/20210410/201190354fkmDPI0As.png](https://ithelp.ithome.com.tw/upload/images/20210410/201190354fkmDPI0As.png)





-----
ComboBox下拉式方塊的事件

按閃電找到SelectedIndexChanged選取的項目被改變-點2下
![https://ithelp.ithome.com.tw/upload/images/20210410/20119035dIOhDFKwUV.png](https://ithelp.ithome.com.tw/upload/images/20210410/20119035dIOhDFKwUV.png)

![https://ithelp.ithome.com.tw/upload/images/20210410/20119035F7JlKljsdg.png](https://ithelp.ithome.com.tw/upload/images/20210410/20119035F7JlKljsdg.png)

TextChanged內容文字被改變
![https://ithelp.ithome.com.tw/upload/images/20210410/20119035clqI6mtLcr.png](https://ithelp.ithome.com.tw/upload/images/20210410/20119035clqI6mtLcr.png)



要特別注意的是除了switch的規格符合之外,也要先對 year和month作動,按
    private void Cbx_BDY_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void Cbx_BDY_TextChanged(object sender, EventArgs e)
    {

    }

    private void Cbx_BDM_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void Cbx_BDM_TextChanged(object sender, EventArgs e)
    {

    }



-----



程式結構:
修飾字 程序型態 程序名稱 (引數1 型態 引數1 名稱,引數2 型態 引數2 名稱.... )

美行結尾加分號;

-----



副程式(會被重複呼叫)結構:

修飾字 程序型態 程序名稱 (引數1 型態 引數1 名稱,引數2 型態 引數2 名稱.... )
private void vSetDays()
{
中間貼上月份程式
}


-----


副程式(會被重複呼叫)
vSetDays();



-----
加入combox的刪除重複.才不會一直新增
cbx_BDD.Items.Clear();
![https://ithelp.ithome.com.tw/upload/images/20210410/20119035zaAGfSBmox.png](https://ithelp.ithome.com.tw/upload/images/20210410/20119035zaAGfSBmox.png)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _20210407
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Btn_Opt_Click(object sender, EventArgs e)
    {
        string strOpt = "";
        if (tbx_FN.Text != "")
        { strOpt = "姓名" + tbx_FN.Text + "\n"; }


        if (rdbtn_BTA.Checked == true)
        { strOpt += "血型" + rdbtn_BTA.Text + "\n"; }
        else if (rdbtn_BTB.Checked == true)
        { strOpt += "血型" + rdbtn_BTB.Text + "\n"; }
        else if (rdbtn_BTO.Checked == true)
        { strOpt += "血型" + rdbtn_BTO.Text + "\n"; }
        else
        { strOpt += "血型" + rdbtn_BTAB.Text + "\n"; }

        if (rdbtn_GDM.Checked == true)
        {
            strOpt += "性別:" + rdbtn_GDM.Text + "\n";
        }
        else
        {
            strOpt += "性別" + rdbtn_GDF.Text + "\n";

        }

        if (ckbx_1.Checked == true || ckbx_2.Checked == true || ckbx_3.Checked == true
            || ckbx_4.Checked == true || ckbx_5.Checked == true || ckbx_6.Checked == true)
        {
            strOpt += "興趣";
            if (ckbx_1.Checked == true) { strOpt += ckbx_1.Text + ""; }
            if (ckbx_2.Checked == true) { strOpt += ckbx_2.Text + ""; }
            if (ckbx_3.Checked == true) { strOpt += ckbx_3.Text + ""; }
            if (ckbx_4.Checked == true) { strOpt += ckbx_4.Text + ""; }
            if (ckbx_5.Checked == true) { strOpt += ckbx_5.Text + ""; }
            if (ckbx_6.Checked == true) { strOpt += ckbx_6.Text + ""; }
        }




        lab_Opt.Text = strOpt;

    }

    private void Form1_Load(object sender, EventArgs e)
    {
      

        for (int i = 1; i <= 12; i++)
        {
            cbx_BDM.Items.Add(i);
        }

        int iYoT=DateTime.Today.Year;

        for (int i = iYoT; i >= iYoT - 130; i--)
        {
            cbx_BDY.Items.Add(i);
        }

        cbx_BDY.Text = (iYoT - 16).ToString();

        vSetDays();



    }

    private void Cbx_BDY_SelectedIndexChanged(object sender, EventArgs e)
    {
        vSetDays();
    }

    private void Cbx_BDY_TextChanged(object sender, EventArgs e)
    {
        vSetDays();
    }

    private void Cbx_BDM_SelectedIndexChanged(object sender, EventArgs e)
    {
        vSetDays();
    }

    private void Cbx_BDM_TextChanged(object sender, EventArgs e)
    {
        vSetDays();
    }
    private void vSetDays()
    {
        cbx_BDD.Items.Clear();

        int iDays = 0, iMth = int.Parse(cbx_BDM.Text), iYr = 0;

        switch (iMth)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                iDays = 31;
                break;


            case 4:
            case 6:
            case 9:
            case 11:
                iDays = 30;
                break;

            case 2:
                iYr = int.Parse(cbx_BDY.Text);
                if (iYr % 400 == 0 || (iYr % 4 == 0 && iYr % 100 != 0))
                { iDays = 29; }
                else
                { iDays = 28; }

                break;
        }

        for (int i = 1; i <= iDays; i++)
        {
            cbx_BDD.Items.Add(i);
        }

    }
}

}




-----
原~

等一下六點就要出遠門
所以現在先來發

現在12點了外面還是有人很熱鬧的在放煙火
我想到追蹤一年時也夠久了

只是還是不想被放棄![/images/emoticon/emoticon20.gif](/images/emoticon/emoticon20.gif)

其實C#我看很多工廠都有在用
應該是用在與PLC結合的圖形化界面
![/images/emoticon/emoticon14.gif](/images/emoticon/emoticon14.gif)

不然就是IOT
不過這些應該都不是我目前這個等級能做的

C# 連結資料庫 修改資料庫資料(INSERT, UPDATE, DELETE)
1. using System.Data.SqlClient; 要加入這行喔!!

2. 資料庫連接範例 INSERT, UPDATE, DELETE 語法(此範例使用的是INSERT)
程式碼:
private void DBSelectUpdateDelect()
{
        //要連接資料庫所需要的 SqlConnection 物件
        sqlConnection dataConnection = new SqlConnection();
        
       
       
       
這個應該可以![/images/emoticon/emoticon08.gif](/images/emoticon/emoticon08.gif)








DEAR ALL 希望我明天趕得急12點前回來發文
不然就GG了

上一篇
C#實作-比大小
下一篇
應該還是實作~
系列文
C#可以做出甚麼?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言